package cn.sinata.wanglin.aliyunoss;
import android.util.Log;
import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.xilada.xldutils.utils.Utils;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import rx.Observable;
import rx.Subscriber;

/**
 * description：
 * Created by kaihongwu on 2016/12/22 下午 10:32
 */
public class UpLoadImageUtils {
    private int number=0;
    private List<OssBean> mOssBeans;
    private  Ossinterface mOssinterface;
    private List<OssBean> mRetOssBeans;
    public  UpLoadImageUtils(List<OssBean> mOssBeans) {
        this.mOssBeans = mOssBeans;
        mRetOssBeans=new ArrayList<>();
    }
    public void init(Ossinterface mOssinterface){
        this.mOssinterface = mOssinterface;
        asyncPutOneImgFromLocalFile(mOssBeans.get(number));
    }
    public Observable<List<OssBean>> uploadimg(){
        return   Observable.create(new Observable.OnSubscribe<List<OssBean>>() {
            @Override
            public void call(final Subscriber<? super List<OssBean>> subscriber) {
                init(new Ossinterface() {
                    @Override
                    public void onSuccess(List<OssBean> mOssBeans) {
                        subscriber.onStart();
                        subscriber.onNext(mOssBeans);
                        subscriber.onCompleted();
                    }

                    @Override
                    public void onFailure() {
                        List<OssBean> mRetOssBeans=new ArrayList<>();
                        subscriber.onStart();
                        subscriber.onNext(mRetOssBeans);
                        subscriber.onCompleted();
                    }
                });
            }
        });
    }

    // 从本地文件上传，使用非阻塞的异步接口
    public void asyncPutOneImgFromLocalFile(OssBean mOssBean) {
        number++;
        String urlkey= getOnlyUrlKey();
        //还回拼接好以后的地址
        mRetOssBeans.add(new OssBean(OssMyUtils.imgurlhead+urlkey, mOssBean.getPath()));
        // 构造上传请求
        PutObjectRequest put = new PutObjectRequest(OssMyUtils.myBucket,urlkey, mOssBean.getPath());
        OssMyUtils.oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                // 这里进行递归单张图片上传，在外面判断是否进行跳出
                if (number <= mOssBeans.size() - 1) {
                    asyncPutOneImgFromLocalFile(mOssBeans.get(number));
                }else{
                    //全部上传成功
                    mOssinterface.onSuccess(mRetOssBeans);
                    return;
                }

            }

            @Override
            public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                // 请求异常
                if (clientExcepion != null) {
                    // 本地异常如网络异常等
                    clientExcepion.printStackTrace();
                }
                if (serviceException != null) {
                    // 服务异常
                    Log.e("ErrorCode", serviceException.getErrorCode());
                    Log.e("RequestId", serviceException.getRequestId());
                    Log.e("HostId", serviceException.getHostId());
                    Log.e("RawMessage", serviceException.getRawMessage());
                }
                Log.d("--全部上传--", "onFailure");
                mOssinterface.onFailure();
                return;
            }
        });
    }
    public String getOnlyUrlKey(){
        String urlkey=null;
        String str=UUID.randomUUID().toString().replaceAll("-", "");
        String strTime=""+System.currentTimeMillis();
        urlkey=str+"--"+strTime;
        Utils.systemErr(urlkey);
        return urlkey;
    }
}
